package bg.tu.varna.pms.client.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingUtilities;

import bg.tu.varna.pms.client.ApplicationContext;
import bg.tu.varna.pms.client.gui.ContactListFrame;
import bg.tu.varna.pms.client.listener.UserEventImpl;

public class LoginRegisterController implements ActionListener,
		ApplicationEvent {

	private ContactListFrame frame;

	public LoginRegisterController() {
		init();
	}
	
	private void init() {
		ApplicationContext.getApplicationContext().addApplicationEventListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		final String command = e.getActionCommand();

		if (MypeActionCommand.SHOW_REGISTER.equals(command)) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					frame.showRegister(LoginRegisterController.this);
				}
			});
		} else if (MypeActionCommand.REGISTER.equals(command)) {
			ApplicationContext.getApplicationContext().register(frame.getUsernameRegister(),
					frame.getPasswordRegister());
		} else if (MypeActionCommand.SIGN_IN.equals(command)) {
			ApplicationContext.getApplicationContext()
					.login(frame.getUsernameLogin(), frame.getPasswordLogin(), 0);
		}
	}

	public void startTheShow() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				frame = new ContactListFrame(LoginRegisterController.this);
				frame.setVisible(true);
			}
		});

	}

	@Override
	public void loginComplete(boolean isOK) {
		if (isOK) {
			showContactList();
		} else {
			showError("Login not OK. Try again!");
		}
	}

	@Override
	public void logoutComplete(boolean isOK) {
		if (isOK) {
			showLogin();
		} else {
			showError("Logout not OK. Try again!");
		}
	}

	@Override
	public void registrationComplete(boolean isOK) {
		if (isOK) {
			showLogin();
		} else {
			showError("Registration not OK. Try again!");
		}
	}

	private void showLogin() {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				frame.showLogin(LoginRegisterController.this);
			}
		});
	}
	
	private void showContactList() {
		final UserGroupController userGroupController = new UserGroupController(frame.getUserGroupPanel());
		final ContactlistController controller = new ContactlistController(frame.getContactListPanel());
		UserEventImpl.getInstance().addContactlistListener(controller);
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				frame.showContactList(userGroupController, controller);
			}
		});
	}

	@Override
	public void loginError(Exception e) {
		// TODO Remove
		showContactList();
		showError(e.getMessage());
	}

	@Override
	public void logoutError(Exception e) {
		showError(e.getMessage());
	}

	@Override
	public void registrationError(Exception e) {
		showError(e.getMessage());
	}
	
	private void showError(String message) {
		frame.showErrorMessage(message);
	}
}
